package com.sgb.kjwl.viewmodel.self.carMakers;

import android.app.Application;
import android.content.Context;

import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.alimaplibrary.util.ToastUtil;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.self.MyOfferEntity2;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.dialog.DialogHelper;

import java.util.List;
import java.util.Map;

/**
 * @author: 黄淋
 * @date: 2021/8/26 16:42
 * @Description: 我的接单内容页viewModel
 */
public class MyOrderFragModel extends AndroidViewModel {
    public MutableLiveData<ListResponse<List<MyOfferEntity2>>> ldRecord = new MutableLiveData<>();
    private int page = 1;

    public MyOrderFragModel(Application application) {
        super(application);
    }

    public void getMyOrderOrder(Context context, int status, boolean isRefresh, Map<String, Object> map) {
        if (isRefresh) {
            page = 1;
        } else
            page++;
        DialogHelper.showProgressDialog(context, "数据加载中...");
        map.put("page", page);
        map.put("limit", 15);
        //竞价货源报价0、2接单 普通货源报价3
        map.put("searchType", 2);
        //货源模式 0 普通模式 1 竞价模式
        map.put("pattern", 1);
        if (status != 0)
            map.put("status", status);

        NewAPIManager.getInstance().getLogisticsAPI().getMyOfferList(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<MyOfferEntity2>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<MyOfferEntity2>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    ldRecord.postValue(data.getData());
                    if (data.getData().haveMoreData()) {
                        page = data.getData().nextPage();
                    }
                } else {
                    ToastUtil.show(context, data.getMsg());
                }
            }

            @Override
            public void onFail() {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

}
